﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生成绩管理系统
{
    public partial class FrmaddUser : Form
    {
        public FrmaddUser()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)//添加用户
        {
            string userid = this.txtUserid.Text;
            string userpwd = this.txtUserpwd.Text;
            string qpwd = this.txtQPwd.Text;
            string userlervel = (string)this.combUserlervel.SelectedItem;
            if (userid == null || userid.Trim().Equals(""))
            {//用户名不允许为空
                MessageBox.Show("请输入用户名！","提示");
                this.txtUserid.Focus();
                return;
            }
            if(userpwd!=qpwd)
            {//判断两次输入的密码是否一致
                MessageBox.Show("密码不一致","提示");
                this.txtUserpwd.Focus();
                return;
            }
            UserInfoData data = new UserInfoData();
            data.Userid = userid;//获取用户名
            data.Userpwwd = userpwd;//获取用户密码
            data.Userlevel = userlervel;//获取用户身份
            try
            {
                if (UserInfoOperation.insertUserInfo(data))
                {
                    MessageBox.Show("成功添加！", "提示");
                    this.txtUserid.Text = "";//清空文本框
                    this.txtUserpwd.Text = "";
                    this.txtQPwd.Text = "";
                }
                else
                {
                    MessageBox.Show("添加失败！","错误");
                }
            }
            catch(Exception ex)
            {
                ex.ToString();
                MessageBox.Show("保存失败！","错误");
            }
        }

        private void btnCannel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmaddUser_Load(object sender, EventArgs e)
        {
            string ConnectionSttring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            SqlConnection cn = new SqlConnection(ConnectionSttring);
            cn.Open();
            string str = "select 用户权限 from 用户表 group by 用户权限";
            SqlCommand cm = new SqlCommand(str, cn);
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                this.combUserlervel.Items.Add(dr[0]);
            }
            cm.Dispose();
            cn.Close();
            cn.Dispose();
        }
    }
}